<template>
    <div class="app-container">
      <br/>
      <!--条件筛选区-->
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="70px"
               class="condition_form">

        <el-row >
          <el-col :span="20">
            <!--根据用户名模糊查询-->
            <el-form-item label="用户名" prop="username">
              <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable
                        @keyup.enter.native="handleQuery"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="serch-button">
              <el-button type="primary" icon="el-icon-search" size="large" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="large" @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-divider></el-divider>

      <!--增删操作区-->
      <el-row :gutter="20" style="float: right; margin-bottom: 20px">
        <!--批量删除-->
        <el-col :span="6">
          <el-button type="danger" plain icon="el-icon-delete" :disabled="multiple"
                     @click="handleDelete">批量删除
          </el-button>
        </el-col>
      </el-row>
      <br/>
      <!--数据展示区-->
      <el-table v-loading="loading" :data="logList" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="40" align="center"/>
        <el-table-column label="ID" align="center" prop="id" width="60px"/>
        <el-table-column label="用户名" align="center" prop="username" width="70px"/>
        <el-table-column label="接口描述" align="center" prop="businessName"/>
        <el-table-column label="请求路径" align="center" prop="requestUrl"/>
        <el-table-column label="请求方式" align="center" prop="requestMethod">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.requestMethod === 'GET'" effect="dark">{{ scope.row.requestMethod }}</el-tag>
            <el-tag v-if="scope.row.requestMethod === 'POST'" type="success" effect="dark">{{ scope.row.requestMethod }}</el-tag>
            <el-tag v-if="scope.row.requestMethod === 'PUT'" type="info" effect="dark">{{ scope.row.requestMethod }}</el-tag>
            <el-tag v-if="scope.row.requestMethod === 'DELETE'" type="danger" effect="dark">{{ scope.row.requestMethod }}</el-tag>
          </template>
        </el-table-column>
<!--        <el-table-column label="操作系统" align="center" prop="operationName">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag type="success">{{ scope.row.operationName }}</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column label="浏览器" align="center" prop="browserName">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag type="info">{{ scope.row.browserName }}</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="IP地址" align="center" prop="ip"/>
        <el-table-column label="地址 运营商" align="center" prop="source" >
          <template slot-scope="scope">
            <el-tag>{{ scope.row.source }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理耗时" align="center" prop="spendTime">
          <template slot-scope="scope">
            {{scope.row.spendTime}} 毫秒
          </template>
        </el-table-column>
        <el-table-column label="请求时间" align="center" prop="createTime"/>
        <!--      <el-table-column label="全限定名（类）" align="center" prop="classPath"/>-->
        <el-table-column label="方法名" align="center" prop="methodName"/>


        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-divider></el-divider>

      <!--页数-->
      <el-pagination :page-size.sync="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="total" :page-sizes="[5,10, 20, 30, 40]" :current-page.sync="queryParams.pageNum"
                     @current-change="getList"
                     @size-change="getList"/>
    </div>
  </template>

  <script>
  import {getPageLog, deleteLog} from '@/api/log'

  export default {
    name: 'User',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户表格数据
        logList: null,
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          //当前页数
          pageNum: 1,
          //每页大小
          pageSize: 10,
          //用户名
          username: ''
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        token: {
          token: ''
        },
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询用户列表 */
      getList() {
        this.loading = true
        getPageLog(this.queryParams).then((response) => {
          this.logList = response.data.list;
          this.total = response.data.total;
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.queryParams = {
          //当前页数
          pageNum: 1,
          //每页大小
          pageSize: 10,
          //用户名
          username: ''
        }
        this.getList()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$alert('是否确认删除日志编号为"' + ids + '"的数据项？', '提示', {
          confirmButtonText: '确定',
          callback: action => {
            return deleteLog(ids).then(() => {
              this.getList()
              this.$notify({
                title: '操作成功',
                message: '删除日志成功！',
                type: 'success'
              });
            }).catch(() => {
              this.$notify.error({
                title: '操作失败',
                message: '删除日志失败'
              });
            })
          }
        })
      }
    }
  }
  </script>

  <style scoped>
  .condition_form .el-form-item__label {
    font-size: 14px;
  }

  .app-container {
    margin-left: 20px;
    margin-top: 20px;
  }
  </style>
